package com.cys.markdown.doc.project.service;

import com.cys.common.util.base.IdWorker;
import com.cys.common.util.base.StringUtil;
import com.cys.markdown.doc.project.interfaces.dao.ProjectDocDao;
import com.cys.markdown.doc.project.interfaces.dao.ProjectInfoDao;
import com.cys.markdown.doc.project.interfaces.dao.ProjectRelUserDao;
import com.cys.markdown.doc.project.interfaces.entity.IProjectDoc;
import com.cys.markdown.doc.project.entity.ProjectDoc;
import com.cys.markdown.doc.project.interfaces.entity.IProjectInfo;
import com.cys.markdown.doc.project.interfaces.entity.IProjectRelUser;
import com.cys.markdown.doc.project.interfaces.service.ProjectDocService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cys.common.util.base.MapUtil;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.annotation.Resource;

/**
 * T_PROJECT_DOC【项目文档表】 Service接口实现类
 *
 * @author caiyushen
 */
@Service("projectDocServiceImpl")
public class ProjectDocServiceImpl implements ProjectDocService {

    private static Logger logger = LoggerFactory.getLogger(ProjectDocServiceImpl.class);

    @Autowired
    private ProjectDocDao projectDocDao;

    @Autowired
    private ProjectRelUserDao projectRelUserDao;

    @Autowired
    private ProjectInfoDao projectInfoDao;


    /**
     * 新增ProjectDoc
     *
     * @param params ProjectDoc实体类对应参数
     */
    @Override
    public IProjectDoc save(Map<String, Object> params) throws Exception {
        if (!StringUtil.checkObj(params)) {
            logger.error("method[save] params is null.");
            return null;
        }
        IProjectDoc projectDoc = (IProjectDoc) MapUtil.toObject(ProjectDoc.class, params);
        projectDoc.setProjectDocId(IdWorker.getId());
        projectDocDao.save(projectDoc);
        return projectDoc;
    }

    /**
     * 新增ProjectDoc
     *
     * @param projectDoc ProjectDoc实体类
     * @return ProjectDoc实体类
     */
    @Override
    public IProjectDoc save(IProjectDoc projectDoc) {
        if (!StringUtil.checkObj(projectDoc)) {
            logger.error("method[save] projectDoc is null.");
            return null;
        }
        projectDoc.setProjectDocId(IdWorker.getId());
        projectDocDao.save(projectDoc);
        return projectDoc;
    }

    /**
     * 修改ProjectDoc
     *
     * @param params ProjectDoc实体类对应参数
     */
    @Override
    public IProjectDoc update(Map<String, Object> params) throws Exception {
        IProjectDoc projectDoc2 = new ProjectDoc();
        projectDoc2.setProjectDocName("新增22");
        this.save(projectDoc2);

    /*    if (!StringUtil.checkObj(params)) {
            logger.error("method[update] params is null.");
            throw new RuntimeException();
//            throw new Exception();
//            return null;
        }*/
        Long id = MapUtil.getLong(params, "projectDocId");
        if (!StringUtil.checkObj(id)) {
            logger.error("method[update] params.id is null.");
            return null;
        }
        IProjectDoc projectDoc = (IProjectDoc) MapUtil.toObject(ProjectDoc.class, params);
        projectDocDao.update(projectDoc);
        return projectDoc;
    }

    /**
     * 修改ProjectDoc
     *
     * @param projectDoc ProjectDoc实体类
     */
    @Override
    public IProjectDoc update(IProjectDoc projectDoc) {
        if (!StringUtil.checkObj(projectDoc)) {
            logger.error("method[update] projectDoc is null.");
            return null;
        }
        if (!StringUtil.checkObj(projectDoc.getProjectDocId())) {
            logger.error("method[update] params.projectDocId is null.");
            return null;
        }
        projectDocDao.update(projectDoc);
        return projectDoc;
    }

    /**
     * 通过id物理删除ProjectDoc
     *
     * @param id ProjectDoc主键
     */
    @Override
    public void deleteById(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[deleteById] id is null.");
            return;
        }
        projectDocDao.deleteById(id);
    }

    /**
     * 批量物理删除ProjectDoc
     *
     * @param ids ProjectDoc主键数组
     */
    @Override
    public void deleteBatch(Long[] ids) {
        if (!StringUtil.checkObj(ids)) {
            logger.warn("method[deleteBatch] ids is null.");
            return;
        }
        projectDocDao.deleteBatch(ids);
    }

    /**
     * 通过id逻辑删除ProjectDoc
     *
     * @param id ProjectDoc主键
     */
    @Override
    public void logicDeleteById(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[logicDeleteById] id is null.");
            return;
        }
        projectDocDao.logicDeleteById(id);
    }

    /**
     * 批量逻辑删除ProjectDoc
     *
     * @param ids ProjectDoc主键数组
     */
    @Override
    public void logicDeleteBatch(Long[] ids) {
        if (!StringUtil.checkObj(ids)) {
            logger.warn("method[logicDeleteBatch] ids is null.");
            return;
        }
        projectDocDao.logicDeleteBatch(ids);
    }


    /**
     * 根据id加载ProjectDoc
     *
     * @param id ProjectDoc主键
     */
    @Override
    public IProjectDoc find(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[find] id is null.");
            return null;
        }
        return projectDocDao.findEntityById(id);
    }

    /**
     * 根据id以及登录用户id加载ProjectDoc
     *
     * @param id        ProjectDoc主键
     * @param sysUserId 用户主键
     * @return ProjectDoc实体类
     */
    @Override
    public IProjectDoc findBySysUserId(Long id, Long sysUserId) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[findBySysUserId] id is null.");
            return null;
        }
        if (!StringUtil.checkObj(sysUserId)) {
            logger.warn("method[findBySysUserId] sysUserId is null.");
            return null;
        }
        IProjectDoc projectDoc = projectDocDao.findEntityById(id);
        if (!StringUtil.checkObj(projectDoc)) {
            logger.warn("method[findBySysUserId] projectDoc is null. id:" + id);
            return null;
        }

        IProjectInfo projectInfo = projectInfoDao.findEntityById(projectDoc.getProjectInfoId());
        if (!StringUtil.checkObj(projectInfo)) {
            logger.warn("method[findBySysUserId] projectInfo is null. id:" + projectDoc.getProjectInfoId());
            return null;
        }
        Map param = new HashMap();
        param.put("projectInfoId", StringUtil.checkObj(projectInfo.getProjectParentId()) ? projectInfo.getProjectParentId() : projectInfo.getProjectInfoId());
        param.put("sysUserId", sysUserId);
        IProjectRelUser rel = projectRelUserDao.findEntityByUserIdAndProId(param);

        if (!StringUtil.checkObj(rel)) {
            logger.warn("method[findBySysUserId] rel is null. sysUserId:" + sysUserId + " projectInfoId:" + MapUtil.getString(param, "projectInfoId"));
            return null;
        }
        return projectDoc;
    }

    /**
     * 获取ProjectDoc列表数据
     *
     * @param map 默认空数据集
     */
    @Override
    public List<IProjectDoc> findList(Map<String, Object> map) {
        return (List<IProjectDoc>) projectDocDao.findList(map);
    }


    /**
     * 分页获取数据
     *
     * @param pageNo   当前页
     * @param pageSize 当前页数
     * @param params   检索参数
     */
    @Override
    public PageInfo<IProjectDoc> findPage(Integer pageNo, Integer pageSize, Map<String, Object> params) {
        if (!StringUtil.checkObj(pageNo)) {
            logger.warn("method[findPage] pageNo is null. pageNo = 1");
            pageNo = 1;
        }
        if (!StringUtil.checkObj(pageSize)) {
            logger.warn("method[findPage] pageSize is null. pageSize = 5");
            pageSize = 5;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<IProjectDoc> dataList = projectDocDao.findPage(params);
        PageInfo<IProjectDoc> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

    /**
     * 根据用户主键、项目主键获取分页列表
     *
     * @param pageNo                当前页
     * @param pageSize              当前页数
     * @param params-projectInfoId  项目主键
     * @param params-sysUserId      用户主键
     * @param params-projectDocName 文档名称
     * @return 分页数据
     */
    @Override
    public PageInfo<IProjectDoc> findPageBySysUserId(Integer pageNo, Integer pageSize, Map params) {
        Long projectInfoId = MapUtil.getLong(params, "projectInfoId");
        if (!StringUtil.checkObj(projectInfoId)) {
            logger.warn("method[findPageBySysUserId] projectInfoId is null.");
            return null;
        }
        if (!StringUtil.checkObj(params.get("sysUserId"))) {
            logger.warn("method[findPageBySysUserId] sysUserId is null.");
            return null;
        }
        IProjectInfo projectInfo = projectInfoDao.findEntityById(projectInfoId);
        if (!StringUtil.checkObj(projectInfo)) {
            logger.warn("method[findPageBySysUserId] projectInfo is null.");
            return null;
        }
        if (StringUtil.checkObj(projectInfo.getProjectParentId())) {
            //如果是子项目，则需要改成父级项目主键
            params.put("projectInfoId", projectInfo.getProjectParentId());
        }
        //根据项目主键以及用户主键获取关联关系
        IProjectRelUser projectRelUser = projectRelUserDao.findEntityByUserIdAndProId(params);
        if (!StringUtil.checkObj(projectRelUser)) {
            logger.warn("method[findPageBySysUserId] projectRelUser is null.");
            return null;
        }
        List<Long> projectInfoIdList = new ArrayList<>();
        projectInfoIdList.add(projectInfoId);
        //该用户已经有项目权限，根据项目id获取所有子项目主键
        List<IProjectInfo> childrenProjectList = projectInfoDao.findListByParentId(projectInfoId);
        if (!CollectionUtils.isEmpty(childrenProjectList)) {
            for (IProjectInfo childrenProject : childrenProjectList) {
                projectInfoIdList.add(childrenProject.getProjectInfoId());
            }
        }
        params.put("projectInfoIdList", projectInfoIdList);
        if (!StringUtil.checkObj(pageNo)) {
            logger.warn("method[findPageBySysUserId] pageNo is null. pageNo = 1");
            pageNo = 1;
        }
        if (!StringUtil.checkObj(pageSize)) {
            logger.warn("method[findPageBySysUserId] pageSize is null. pageSize = 5");
            pageSize = 5;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<IProjectDoc> dataList = projectDocDao.findPageByProjectInfoIdList(params);
        PageInfo<IProjectDoc> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

}
 



